package studychinese.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.hschinese.pronunciation.activity.MyApplication;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.qqtheme.framework.picker.OptionPicker;
import studychinese.callback.DialogCallback;
import studychinese.config.AppUrl;
import studychinese.config.Url;
import studychinese.model.NativeLanguageBean;
import studychinese.model.RegisterBean;
import studychinese.model.UserBean;
import studychinese.utils.SPUtils;
import studychinese.utils.ToastUtils;


/**
 * A simple {@link Fragment} subclass.
 */
public class NativeLanguageFragment extends BaseFragment {


    @Bind(R.id.tv_native_titile)
    TextView tvNativeTitile;
    @Bind(R.id.rl_native_seletelanguage)
    RelativeLayout rlNativeSeletelanguage;

    public static NativeLanguageFragment newInstance(String title) {

        Bundle args = new Bundle();
        args.putString("title", title);
        NativeLanguageFragment fragment = new NativeLanguageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_native_language, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    public void init() {

        OkGo.<NativeLanguageBean>post(AppUrl.getAppUrl(Url.REGISTER_USERMOTHERTONGUE))
                .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)
                .execute(new DialogCallback<NativeLanguageBean>(getActivity(),true) {
                    @Override
                    public void onSuccess(Response<NativeLanguageBean> response) {
                                 NativeLanguageBean body = response.body();
                                 if(body.getState().equals("0")){
                                     List<NativeLanguageBean.DataBean> data = body.getData();
                                     if(data.size()>0 ){
                                         setView(data);
                                     }else {
                                         ToastUtils.showShort(body.getMessage());
                                     }

                                 }
                             }
                         }
                );

        

    }

    private void setView(final List<NativeLanguageBean.DataBean> data) {
        final List<String> list = new ArrayList<>();
        list.clear();
        if(data.size()>0){
            for (int i = 0; i <data.size() ; i++) {
                list.add(data.get(i).getName());
            }
        }

        OptionPicker picker = new OptionPicker(getActivity(),list);
       /* picker.setCanceledOnTouchOutside(false);
        picker.setDividerRatio(WheelView.DividerConfig.FILL);*/

        picker.setSelectedIndex(0);
        picker.setCycleDisable(true);
        picker.setOffset(2);
        picker.setDividerVisible(true);
        picker.setLineSpaceMultiplier(4);
        picker.setDividerColor(Color.WHITE);
        picker.setTextSize(18);
        picker.setLineColor(Color.WHITE);
        picker.setTextColor(Color.WHITE,Color.WHITE);
        picker.setCycleDisable(true);

        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener(

        ) {
            @Override
            public void onOptionPicked(int index, String item) {
                // showToast();
                // ToastUtils.showShort("index=" + index + ", item=" + item);
            }
        });
       final RegisterBean.DataBean infor = UserBean.getInfor();
        if(infor!=null){
            infor.setLang(data.get(picker.getSelectedIndex()).getId());
            SPUtils.putObject(MyApplication.getInstance(), "userInfo", "userInfo",infor);

        }
        picker.setOnWheelListener(new OptionPicker.OnWheelListener() {
            @Override
            public void onWheeled(int index, String item) {

                if(infor!=null){
                    if(data.size()>0){
                        infor.setLang(data.get(index).getId());
                        SPUtils.putObject(MyApplication.getInstance(), "userInfo", "userInfo",infor);
                    }

                }

            }
        });
        //得到选择器视图，可内嵌到其他视图容器，不需要调用show方法
        View pickerContentView = picker.getContentView();
        rlNativeSeletelanguage.addView(pickerContentView);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
